/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.graphs3d;

import java.awt.AWTEvent;
import java.awt.event.KeyEvent;
import java.util.Enumeration;
import java.util.Vector;
import javax.media.j3d.Behavior;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.WakeupOnAWTEvent;

public class keyRotationBehavior extends Behavior {

    private TransformGroup targetTG;
    private Transform3D rotationY = new Transform3D();
     private Transform3D transformGroupLocation = new Transform3D();
    private Transform3D rotationX = new Transform3D();
    private Transform3D rotationText = new Transform3D();
    private Transform3D textRotationX = new Transform3D();
    private Transform3D textRotationY = new Transform3D();
    private double angleY = 0.0,  angleX = 0.0,  angleTextX = 0.0,  angleTextY = 0.0;
    private WakeupOnAWTEvent AWTEventCondition;
    private Vector<TransformGroup> textsGroup;
    // create keyRotationBehavior

    keyRotationBehavior(TransformGroup targetTG, Vector<TransformGroup> texts) {
        this.targetTG = targetTG;
        targetTG.getTransform(transformGroupLocation);
        this.textsGroup = texts;
        for (int i = 0; i < textsGroup.size(); i++) {
            textsGroup.get(i).setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
            textsGroup.get(i).setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        }
        AWTEventCondition = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED);
    }

    // initialize the Behavior
    //     set initial wakeup condition
    //     called when behavior beacomes live
    public void initialize() {
        // set initial wakeup condition
        this.wakeupOn(AWTEventCondition);

    }

    // behave
    // called by Java 3D when appropriate stimulus occures
    @Override
    public void processStimulus(Enumeration criteria) {
        // decode event
        AWTEvent[] events = AWTEventCondition.getAWTEvent();
        analizeAction((KeyEvent) events[0]);      
     //   rotationX.rotX(angleX);
        targetTG.getTransform(transformGroupLocation);
        rotationY.rotY(angleY);        
        transformGroupLocation.mul(rotationY);
        targetTG.setTransform(transformGroupLocation);
   //     rotationX.mul(rotationY);
        rotatingText();       
       // targetTG.getTransform(oldLocation);
     //   oldLocation.mul(rotationY);
        //targetTG.setTransform(oldLocation);
        


        this.wakeupOn(AWTEventCondition);
    }

    private void analizeAction(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_DOWN:
                angleX += 0.1;
                angleX %= (2 * Math.PI);
                angleTextX = -0.1;
                angleTextY = 0.0;
                break;
            case KeyEvent.VK_UP:
                angleX -= 0.1 % (2 * Math.PI);
                angleX %= (2 * Math.PI);
                angleTextX = 0.1;
                angleTextY = 0.0;
                break;
            case KeyEvent.VK_LEFT:
                //angleY -= 0.1 % (2 * Math.PI);
                //angleY %= (2 * Math.PI);
                angleY=-0.1;
                angleTextY = 0.1;
                angleTextX = 0.0;
                break;
            case KeyEvent.VK_RIGHT:
                //angleY += 0.1 % (2 * Math.PI);
                //angleY %= (2 * Math.PI);
                angleY=0.1;
                angleTextY = -0.1;
                angleTextX = 0.0;
                break;
        }

    }

    private void rotatingText() {
        //rotating text to face the user at all times
       // textRotationX.rotX(angleTextX);
        textRotationY.rotY(angleTextY);
        //    textRotationX.mul(textRotationY);

        for (int i = 0; i < textsGroup.size(); i++) {
            textsGroup.get(i).getTransform(rotationText);
            rotationText.mul(textRotationY);
            textsGroup.get(i).setTransform(rotationText);
        }
    }
} // end of class SimpleBehavior